var basesUrl ='http://localhost:8080/api';
function http(url){
    return new Promise((resolve,reject)=>{
        wx.request({
            url:basesUrl+url,
            method:"GET",
            dataType:"json",
            responseType:"text",
            success:(res)=>{
              resolve(res)
            },
            error:(err)=>{
              reject(err)
            }
          })
    })
} 

function handleStar(num){
  var arr =[];
  for(var i =0;i<5;i++){
      if(num>=2){
          arr.push(2)
      }else if(num>0){
          arr.push(Number(num.toFixed(1)))
      }else{
          arr.push(0)
      }
      num -= 2;
  }
  return arr
}

function handleStr(str){
  if(str.length>6){
    str = str.slice(0,6)+"..."
}
return str;
}
function bannerHttp(){
    return http('/banner')
}
function getReadHttp(){
    return http('/top250')
}
function getReadDetail(id){
    return http(`/subject?id=${id}`)
}

module.exports = {
    http,
    handleStar,
    handleStr,
    getReadHttp,
    getReadDetail,
    bannerHttp
}